package com.wwwarehouse.common.adapter.media;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.R;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.bean.media.UploadType;
import com.wwwarehouse.common.custom_widget.media.OnItemPlusListener;
import com.wwwarehouse.common.custom_widget.media.OnItemPreviewListener;
import com.wwwarehouse.common.custom_widget.media.OnItemUploadListener;
import com.wwwarehouse.common.tools.ScreenUtils;

import java.util.List;

public class UploadAdapter extends BaseAdapter {
    private Context mContext;
    private int mPadding;

    private List<UploadType> mSelectedUploadTypeList;
    private LayoutInflater mInflater;

    private OnItemPreviewListener mOnItemPreviewListener;
    private OnItemUploadListener mOnItemUploadListener;
    private OnItemPlusListener mOnItemPlusListener;

    //Item类型int值, 必须从0开始依次递增
    public static final int UPLOAD_TYPE_IMAGE = 0;
    public static final int UPLOAD_TYPE_PLUS = 1;
    private static final int UPLOAD_TYPE_COUNT = 2;

    public void setOnItemPreviewListener(OnItemPreviewListener onItemPreviewListener) {
        this.mOnItemPreviewListener = onItemPreviewListener;
    }

    public void setOnItemUploadListener(OnItemUploadListener onItemUploadListener) {
        this.mOnItemUploadListener = onItemUploadListener;
    }

    public void setOnItemPlusListener(OnItemPlusListener onItemPlusListener) {
        this.mOnItemPlusListener = onItemPlusListener;
    }

    public UploadAdapter(Context context, int padding, List<UploadType> selectedUploadTypeList){
        this.mContext = context;
        this.mPadding = padding;

        this.mSelectedUploadTypeList = selectedUploadTypeList;

        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getItemViewType(int position) {
        return mSelectedUploadTypeList.get(position).getType();
    }

    @Override
    public int getViewTypeCount() {
        return UPLOAD_TYPE_COUNT;
    }

    @Override
    public int getCount() {
        return mSelectedUploadTypeList.size();
    }

    @Override
    public Object getItem(int position) {
        return mSelectedUploadTypeList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final UploadType uploadType = mSelectedUploadTypeList.get(position);

        final ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();

            switch (getItemViewType(position)) {
                case UPLOAD_TYPE_IMAGE:
                    convertView = mInflater.inflate(R.layout.item_upload_image_view, null);

                    holder.uploadView = (RelativeLayout) convertView.findViewById(R.id.item_upload_image_view);

                    holder.imageView = (ImageView) convertView.findViewById(R.id.item_image);
                    holder.progressBar = (ProgressBar) convertView.findViewById(R.id.item_progress);
                    holder.fail = (TextView) convertView.findViewById(R.id.item_fail);
                    break;
                case UPLOAD_TYPE_PLUS:
                    convertView = mInflater.inflate(R.layout.item_upload_plus_view, null);

                    holder.plusView = (LinearLayout) convertView.findViewById(R.id.item_upload_plus_view);
                    break;
                default:
                    break;
            }

            //这个地方被坑得不要不要的(不能放在最后)
            AbsListView.LayoutParams params =  new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            params.width = params.height = (ScreenUtils.getScreenWidth(mContext) - mPadding) / 3;
            convertView.setLayoutParams(params);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (holder.imageView != null) {
            BaseApplication.getApplicationInstance()
                    .displayImg("file://" + uploadType.getPath(), holder.imageView);
        }

        if (holder.progressBar != null) {
            if (uploadType.isFinish()) {
                holder.progressBar.setVisibility(View.GONE);
            } else {
                holder.progressBar.setVisibility(View.VISIBLE);
                holder.progressBar.setProgress(uploadType.getProgress());
            }
        }

        if (holder.fail != null && holder.imageView != null) {
            if (uploadType.isSuccess()) {
                //上传成功
                holder.fail.setVisibility(View.GONE);
                holder.imageView.setColorFilter(null);

                //预览图片
                holder.uploadView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnItemPreviewListener != null) {
                            mOnItemPreviewListener.onItemPreview(mSelectedUploadTypeList, position);
                        }
                    }
                });
            } else {
                //上传失败
                holder.fail.setVisibility(View.VISIBLE);
                holder.imageView.setColorFilter(mContext.getResources().getColor(R.color.common_color_upload_fail_99000000));

                //重新上传
                holder.uploadView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        holder.fail.setVisibility(View.GONE);

                        if (mOnItemUploadListener != null) {
                            mOnItemUploadListener.onItemUpload(uploadType.getPath());
                        }
                    }
                });
            }
        }

        if (holder.plusView != null) {
            holder.plusView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemPlusListener != null) {
                        mOnItemPlusListener.onItemPlus();
                    }
                }
            });
        }

        return convertView;
    }

    private class ViewHolder {
        RelativeLayout uploadView;
        ImageView imageView;
        ProgressBar progressBar;
        TextView fail;

        LinearLayout plusView;

    }
}